package com.retailcloud.thirdpartyservice.controller;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.retailcloud.commonutils.exception.RetailCloudException;
import com.retailcloud.commonutils.utils.R;
import com.retailcloud.thirdpartyservice.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wudi
 * @date 2024/11/2
 * @comment
 */
@RestController
@RequestMapping("/wechat")
public class WeChatController {

    @Autowired
    WechatUserService wechatUserService;

    @RequestMapping("/getOpenId")
    public R getOpenId(@RequestParam String code){
        try {
            WxMaJscode2SessionResult openId = wechatUserService.getOpenId(code);
            return R.ok().put("data",openId);
        } catch (Exception e) {
            throw new RetailCloudException("获取openId失败",e);
        }

    }


    @RequestMapping("/getUserInfo")
    public R getUserInfo(   @RequestParam String encryptedData,
                            @RequestParam String iv,
                            @RequestParam String sessionKey){
        try {
            WxMaUserInfo wxMaUserInfo = wechatUserService.getUserInfo(sessionKey,encryptedData,iv);
            return R.ok().put("data",wxMaUserInfo);
        } catch (Exception e) {
            throw new RetailCloudException("获取wxMaUserInfo失败",e);
        }

    }


    @RequestMapping("/getPhoneNumber")
    public R getPhoneNumber(
            @RequestParam String encryptedData,
            @RequestParam String iv,
            @RequestParam String sessionKey) {
        try {
            WxMaPhoneNumberInfo phoneNumber = wechatUserService.getPhoneNumber(encryptedData, iv, sessionKey);
            return R.ok().put("data", phoneNumber);
        } catch (Exception e) {
            throw new RetailCloudException("获取getPhoneNumber失败",e);
        }
    }

}
